<template>
  <view>
  <!-- 拍照组件 -->
  <cameraComponent @setBase64Img="base64img"></cameraComponent>
  <view class="fixed" v-show="certRes==1 ||certRes==0">
  <view v-show="certRes==1"><uni-icons type="checkbox" size="80" color="#00a660"></uni-icons><view class="text themeColor">人脸核验成功，请关闭页面！</view></view>
  <view v-show="certRes==0"><uni-icons type="close" size="80" style="color: red;"></uni-icons><view class="text">人脸核验失败，请核查信息后重试！</view></view>
    <button class="close-btn" @click="toClose" size="mini" v-show="certRes==1 ||certRes==0">关闭页面</button>
	</view>
    <!-- 法定代表人 -->
    <legal-certify ref="legalCertify"></legal-certify>
  </view>
</template>

<script>

import cameraComponent from "./cameraComponent";
import { getToken } from '@/utils/auth'
import {
  companyCerFace,
  companyCerFaceNewCompany,
  personCerFace
} from "../../../api/ec/person/certification/info";
import {updateSession} from "../../../api/userInfo";
import {signFace} from "../../../api/ec/work/contract/contractSign";
import legalCertify from "../../signWeb/legalCertify";
import {goSignPageOrIndexMiXin} from "../../../plugins/goSignPageOrIndexMiXin";

export default {
  mixins: [goSignPageOrIndexMiXin],
  components: {
    cameraComponent,legalCertify
  },
  data() {
    return {
      ruleForm: {},// 二维码扫码参数
      certRes:null,// 验证结果
    };
  },
  onLoad(option) {
    this.ruleForm = option;
  },
  methods: {
    // 获取拍照数据
    base64img(res) {
      this.ruleForm.faceBase64 = res;
      [this.personCerFace, this.companyCerFace, this.companyCerFaceNewCompany,this.signFace][this.ruleForm.verifyType]();
    },
    // 个人认证 人脸
    personCerFace(){
      this.$modal.loading("人脸识别验证中...");
      personCerFace(this.ruleForm).then(response => {
        uni.hideLoading();
        this.$modal.msg("验证成功")
        this.certRes = 1;
        this.checkLogin(1);
      }).catch(error => {
        this.certRes = 0;
      });
    },
    // 法人变更 人脸
    companyCerFace(){
      this.$modal.loading("人脸识别验证中...");
      companyCerFace(this.ruleForm).then(response => {
        let adminInfo = response.data;
        uni.hideLoading();
        this.$modal.msg("验证成功")
        this.certRes = 1;
        // 若是短信打开认证的 则进行登录并自动跳转签署页 若不是同一个人则进行弹窗
        if (!!this.ruleForm.source && !adminInfo.isLegalAndAdmin) {
          setTimeout(() => {
            // 法人人脸完成 弹窗页面
            this.$refs.legalCertify.openDialog(adminInfo)
          }, 500);
        }
      }).catch(error => {
        this.certRes = 0;
      });
    },
    // 法人首次注册公司 人脸
    companyCerFaceNewCompany(){
      this.$modal.loading("人脸识别验证中...");
      companyCerFaceNewCompany(this.ruleForm).then(response => {
        uni.hideLoading();
        this.$modal.msg("验证成功")
        if (!!this.ruleForm.source) {
          setTimeout(() => {
            this.checkLogin(0);
          }, 1000);
        }
        this.certRes = 1;
      }).catch(error => {
        this.certRes = 0;
      });
    },
    // 签署人脸校验
    signFace(){
      this.$modal.loading("人脸识别验证中...");
      signFace(this.ruleForm).then(response => {
        uni.hideLoading();
        this.$modal.msg("验证成功")
        // 若是手机签署 人脸过来的
        if (!!this.ruleForm.source) {
          setTimeout(() => {
            // 保存人脸成功标识
            uni.setStorageSync("signFace" + this.$store.getters.staffInfo.companyId + this.$store.getters.staffInfo.id + this.ruleForm.source, 1);
            uni.navigateBack({delta: 1})
          }, 1000);
        }
        this.certRes = 1;
      }).catch(error => {
        this.certRes = 0;
      });
    },
    /** 关闭当前页面 */
    toClose() {
      if (/MicroMessenger/.test(window.navigator.userAgent)) {
        window.WeixinJSBridge.call('closeWindow')
      }
      if (/AlipayClient/.test(window.navigator.userAgent)) {
        window.AlipayJSBridge.call('closeWebview')
      } else {
        window.open("https://h5.lksign.cn");
      }
    },
    // 判断是否有登录状态
    checkLogin(type) {
      // 判断是否有token 若有 说明是直接手机登录人脸认证的 需要刷新token 并返回首页
      if (getToken()) {
        // 认证完成完成刷新登录用户session信息
        updateSession().then(response => {
          this.$store.dispatch('GetInfo').then((res) => {
            // 检查有无签署合同 无合同 跳转到首页 有合同跳转到盖章页面
            this.goSignPageOrIndex(response.data.mobile,type);
          })
        }).catch(() => {
        });
      }
    },
  },
}
</script>

<style scoped lang="scss">
	.fixed{
		width: 100%;
		text-align:center;
		margin:50% auto 0;
		.text{
			margin-top: 100rpx;
		}
		.close-btn{
			background:$uni-color-primary;
			color:#fff;
			margin-top: 50rpx;
			width:220rpx;
			font-size: 28rpx;
		}
	}

</style>